#define _GNU_SOURCE

#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>

int main(int argc, char **argv) {
    if (argc != 3) {
        printf("usage: test_getaddrinfo host service");
        exit(0);
    }

    struct addrinfo *servinfo, *aip;
    getaddrinfo(argv[1], argv[2], NULL, &servinfo);

    for (aip = servinfo; aip; aip = aip->ai_next) { 
        char ip_addr[INET_ADDRSTRLEN] = "";
        struct sockaddr_in *saip = (struct sockaddr_in*) aip->ai_addr;
        inet_ntop(saip->sin_family, &saip->sin_addr, ip_addr, INET_ADDRSTRLEN);

        printf("ipaddr: %s\n", ip_addr);
        printf("port: %d\n", ntohs(saip->sin_port));
    }

    return 0;
}